package cc.mrbird.febs.pedometer.controller;

import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.pedometer.entity.Chicken;
import cc.mrbird.febs.pedometer.service.IChickenService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * 鸡 列表 Controller
 *
 * @author li
 * @date 2020-02-01 19:38:49
 */
@Slf4j
@Validated
@Controller
public class ChickenController extends BaseController {

    @Autowired
    private IChickenService chickenService;

    @GetMapping("chicken")
    @ResponseBody
    @RequiresPermissions("chicken:list")
    public FebsResponse getAllChickens(Chicken chicken) {
        return new FebsResponse().success().data(chickenService.findChickens(chicken));
    }

    @GetMapping("chicken/list/{batchesId}")
    @ResponseBody
    @RequiresPermissions("chicken:list")
    public FebsResponse chickenList(QueryRequest request, Chicken chicken,
                                    @PathVariable Integer batchesId) {
        Map<String, Object> dataTable = getDataTable(this.chickenService.findChickens(request, chicken, batchesId));
        return new FebsResponse().success().data(dataTable);
    }

    @PostMapping("chicken")
    @ResponseBody
    @RequiresPermissions("chicken:add")
    public FebsResponse addChicken(@Valid Chicken chicken) {
        this.chickenService.createChicken(chicken);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "删除Chicken", exceptionMessage = "删除Chicken失败")
    @GetMapping("chicken/delete")
    @ResponseBody
    @RequiresPermissions("chicken:delete")
    public FebsResponse deleteChicken(Chicken chicken) {
        this.chickenService.deleteChicken(chicken);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改Chicken", exceptionMessage = "修改Chicken失败")
    @PostMapping("chicken/update")
    @ResponseBody
    @RequiresPermissions("chicken:update")
    public FebsResponse updateChicken(Chicken chicken) {
        this.chickenService.updateChicken(chicken);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改Chicken", exceptionMessage = "导出Excel失败")
    @PostMapping("chicken/excel/{batchesId}")
    @ResponseBody
    @RequiresPermissions("chicken:export")
    public void export(QueryRequest queryRequest, Chicken chicken, HttpServletResponse response,
                       @NotBlank(message = "{required}") @PathVariable Integer batchesId) {
        List<Chicken> chickens = this.chickenService.findChickens(queryRequest, chicken,batchesId).getRecords();
        ExcelKit.$Export(Chicken.class, response).downXlsx(chickens, false);
    }

    @GetMapping("chicken/typeList")
    @ResponseBody
    public FebsResponse typeList(){

        return new FebsResponse().success().data(chickenService.typeList());
    }
}
